package com.daka.service.impl;

import com.daka.dao.ActivityMapper;
import com.daka.entity.Activity;
import com.daka.entity.ActivityApply;
import com.daka.entity.Group;
import com.daka.service.*;
import com.daka.service.qnzyz.BHActivityService;
import com.daka.tools.DKQConstant;
import com.daka.tools.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/8/5 0005
 * Time: 14:33
 */
@Service("activityService")
@Transactional
public class ActivityServiceImpl implements ActivityService{

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    BHActivityService bhActivityService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private ActivityApplyService activityApplyService;

    public int insert(Activity activity){
        String address = activity.getAddress().replace(activity.getProvince(), "");
        address = address.replace(activity.getCity(), "");
        address = address.replace(activity.getDistrict(), "");
        if (activity.getProvince().equals(activity.getCity())) {
            address = activity.getProvince() + activity.getDistrict() + address;
        } else {
            address = activity.getProvince() + activity.getCity() + activity.getDistrict() + address;
        }
        activity.setAddress(address);

        //单天
        activity.setType(Activity.TYPE_1);
        activityMapper.insert(activity);

        //向北航提交活动数据
        submit2BH(activity);

        if(activity.getId()==null || activity.getId()==0){
            System.out.println("获取activityId失败!");
        }else{
            System.out.println("成功获取activityId!");
        }
        //新增actibityCode
        update(activity);

        //更新组织活动数量
        Group group = activity.getGroup();
        group.setActvitys(group.getActvitys() + 1);
        groupService.update(group);

        //现场负责人加入活动
        ActivityApply activityApply = new ActivityApply();
        activityApply.setVolunteerId(Long.valueOf(activity.getMonitor()));
        System.out.println("活动id：" + activity.getId());
        activityApply.setActivityId(activity.getId());
        activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
        activityApply.setDelStatus(0);
        activityApply.setRole(DKQConstant.ROLES_ADMIN);
        activityApply.setCreateTime(new Date());
        int a = activityApplyService.insert(activityApply);

        return a;
    }

    public int update(Activity activity){
        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    public Activity selectByPrimaryKey(Long id){
        return activityMapper.selectByPrimaryKey(id);
    }

    public List<Activity> selectByPrimaryKey1(){
        return activityMapper.selectByPrimaryKey1();
    }

    public int delete(Long id){
        return activityMapper.deleteByPrimaryKey(id);
    }

    /**
     * 向北航提交活动数据
     * @param activity
     * @return
     */
    private Activity submit2BH(Activity activity)
    {
        //北航注册活动
        if(StringUtils.isNotBlank(activity.getGroup().getGroupCode()))
        {
            try
            {
                String activityCode = bhActivityService.post(activity);
                if (StringUtils.isEmpty(activityCode))
                    throw new Exception("团中央保存活动信息出错");
                activity.setActivityCode(activityCode);
            } catch (Exception e)
            {
                e.printStackTrace();
                try
                {
                    throw e;
                } catch (Exception e1)
                {
                    e1.printStackTrace();
                }
            }
        }
//        List<String > userIds = new ArrayList<String>();
//        userIds.add(activity.getCreateUser().getMemberCode());
//        userIds.add(activity.getMonitor().getMemberCode());
//        PushGroupMesssage.getInstance().createGroup(PushGroupMesssage.GROUP_TYPE_ACTIVITY,userIds,activity.getActivityCode(),activity.getName());
        return activity;
    }

    public Page<Activity> selectByStatement(Activity activity){
        Page<Activity> page = new Page<Activity>();

//        if(activity.getLimitEnd()==0){
            activity.setLimitEnd(10);
            activity.setLimitStart(10 * activity.getLimitStart());
//        }else{
//            activity.setLimitStart(activity.getLimitStart() * activity.getLimitEnd());
//        }

        List<Activity> list = activityMapper.selectByStatement(activity);
        int count = activityMapper.selectByStatementCount(activity);
        page.setTotalCount(count);
        page.setData(list);
        return page;
    }

    public int selectCountByGroup(Long groupId){
        return activityMapper.selectCountByGroup(groupId);
    }
}







